package com.example.hangzhou.welbeing.mytestdemoapp.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.example.hangzhou.welbeing.mytestdemoapp.R;

import java.util.HashMap;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.tencent.qq.QQ;
import cn.sharesdk.wechat.friends.Wechat;

/**
 * 杭州惟君医疗科技有限公司15
 * Created by 20433 on 2018/3/12
 */

public class SharedSdkActivity extends AppCompatActivity {


    @Bind(R.id.button6)
    Button button6;
    @Bind(R.id.button7)
    Button button7;
    @Bind(R.id.button8)
    Button button8;
    @Bind(R.id.button9)
    Button button9;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shared);
        ButterKnife.bind(this);

    }


    @OnClick({R.id.button6, R.id.button7, R.id.button8, R.id.button9})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.button6:  //微信登录
                Wachtlogin();
                break;
            case R.id.button7:  //qq登录
                QQlogin();
                break;
            case R.id.button8:  //微信分享
                shareWacht();
                break;
            case R.id.button9:  //qq分享
                shareQQ();
                break;
        }
    }

    private void shareQQ() {
        Platform.ShareParams sp = new Platform.ShareParams();
        sp.setShareType(Platform.SHARE_WEBPAGE);
        sp.setTitle("测试分享的标题");
        sp.setTitleUrl("http://sharesdk.cn");// 标题的超链接
        sp.setText("测试分享的文本");
        sp.setImageUrl("http://www.someserver.com/测试图片网络地址.jpg");
        sp.setSite("发布分享的网站名称");
        sp.setSiteUrl("发布分享网站的地址");

        Platform qq =ShareSDK.getPlatform (QQ.NAME);
//Platformqzone = ShareSDK.getPlatform (QZone.NAME);

// 设置分享事件回调（注：回调放在不能保证在主线程调用，不可以在里面直接处理UI操作）
        qq.setPlatformActionListener(new PlatformActionListener() {
            @Override
            public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {

            }

            public void onError(Platform arg0, int arg1, Throwable arg2) {
                //失败的回调，arg:平台对象，arg1:表示当前的动作，arg2:异常信息
            }

            public void onCancel(Platform arg0, int arg1){
                //取消分享的回调
            }
        });
// 执行图文分享
        qq.share(sp);
    }

    private void shareWacht() {
        Platform.ShareParams sp = new Platform.ShareParams();
        sp.setShareType(Platform.SHARE_WEBPAGE);
        sp.setText("测试分享的文本");
        sp.setUrl("你要分享的URL");
        sp.setTitle("你要分享的文本");
        Platform qzone = ShareSDK.getPlatform (Wechat.NAME);
// 设置分享事件回调（注：回调放在不能保证在主线程调用，不可以在里面直接处理UI操作）
        qzone.setPlatformActionListener (new PlatformActionListener() {
            public void onError(Platform arg0, int arg1, Throwable arg2) {
                //失败的回调，arg:平台对象，arg1:表示当前的动作，arg2:异常信息
                Log.e("TAG","失败的回调======");
            }
            public void onComplete(Platform arg0, int arg1, HashMap<String, Object> arg2) {
                //分享成功的回调
                Log.e("TAG","分享成功的回调======");
            }
            public void onCancel(Platform arg0, int arg1) {
                //取消分享的回调
                Log.e("TAG","取消分享的回调======");
            }
        });
// 执行图文分享
        qzone.share(sp);
    }

    private void QQlogin() {
        Platform qzone = ShareSDK.getPlatform(QQ.NAME);
        if(qzone.isAuthValid()) {
            qzone.removeAccount(true);
        }
        qzone.setPlatformActionListener(new PlatformActionListener() {

            @Override
            public void onError(Platform arg0, int arg1, Throwable arg2) {
                // TODO Auto-generated method stub
                arg2.printStackTrace();
               // Toast.makeText(SharedSdkActivity.this, "失败", Toast.LENGTH_SHORT).show();
                Log.e("TAG","失败======"+arg2);

            }

            @Override
            public void onComplete(Platform arg0, int arg1, HashMap<String, Object> arg2) {
                // TODO Auto-generated method stub
                //输出所有授权信息
                arg0.getDb().exportData();
               // Toast.makeText(SharedSdkActivity.this, "成功", Toast.LENGTH_SHORT).show();
                //获取资料
                String UserName = arg0.getDb().getUserName();//获取用户名字
                String Icon = arg0.getDb().getUserIcon(); //获取用户头像
                String token = arg0.getDb().getToken();
                String userID =  arg0.getDb().getUserId();

                Log.e("TAG","UserName======"+UserName);
                Log.e("TAG","Icon======"+Icon);
                Log.e("TAG","token======"+token);
                Log.e("TAG","成功===userID==="+userID);
            }

            @Override
            public void onCancel(Platform arg0, int arg1) {
                // TODO Auto-generated method stub
                //Toast.makeText(SharedSdkActivity.this, "取消", Toast.LENGTH_SHORT).show();
                Log.e("TAG","取消======");
            }
        });
//authorize与showUser单独调用一个即可
        qzone.authorize();//单独授权，OnComplete返回的hashmap是空的

    }

    private void Wachtlogin() {
        Platform Wachzone = ShareSDK.getPlatform(Wechat.NAME);
        if(Wachzone.isAuthValid()) {
            Wachzone.removeAccount(true);
        }
        Wachzone.setPlatformActionListener(new PlatformActionListener() {

            @Override
            public void onError(Platform arg0, int arg1, Throwable arg2) {
                // TODO Auto-generated method stub
                arg2.printStackTrace();
                //oast.makeText(SharedSdkActivity.this, "失败", Toast.LENGTH_SHORT).show();
                Log.e("TAG","失败======");

            }

            @Override
            public void onComplete(Platform arg0, int arg1, HashMap<String, Object> arg2) {
                // TODO Auto-generated method stub
                //输出所有授权信息
                arg0.getDb().exportData();
                //Toast.makeText(SharedSdkActivity.this, "成功", Toast.LENGTH_SHORT).show();
                //获取资料
                String UserName = arg0.getDb().getUserName();//获取用户名字
                String Icon = arg0.getDb().getUserIcon(); //获取用户头像
                String token = arg0.getDb().getToken();

                Log.e("TAG","UserName======"+UserName);
                Log.e("TAG","Icon======"+Icon);
                Log.e("TAG","token======"+token);
                Log.e("TAG","成功======");
            }

            @Override
            public void onCancel(Platform arg0, int arg1) {
                // TODO Auto-generated method stub
               // Toast.makeText(SharedSdkActivity.this, "取消", Toast.LENGTH_SHORT).show();
                Log.e("TAG","取消======");
            }
        });
//authorize与showUser单独调用一个即可
        Wachzone.authorize();//单独授权，OnComplete返回的hashmap是空的
//isValid和removeAccount不开启线程，会直接返回。
    }
}
